package com.jin.stu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jin.stu.domain.Leave;
import com.jin.stu.mapper.LeaveMapper;
import com.jin.stu.service.LeaveService;
import com.jin.stu.vo.LeaveVo;
import com.jin.sys.common.Constast;
import com.jin.sys.common.DataGridView;
import com.jin.sys.common.ResultObj;
import com.jin.sys.common.WebUtils;
import com.jin.sys.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

@Service
@Transactional
public class LeaveServiceImpl extends ServiceImpl<LeaveMapper, Leave> implements LeaveService {


    @Autowired
    private LeaveMapper leaveMapper;

    /**
     * 查询
     */
    @Override
    public DataGridView loadAllLeave(LeaveVo leaveVo) {
        IPage<Leave> leaveIPage = new Page<>(leaveVo.getPage(), leaveVo.getLimit());
        User user = (User) WebUtils.getSession().getAttribute("user");
        QueryWrapper<Leave> queryWrapper = new QueryWrapper<>();
        if (user.getType() == Constast.USER_TYPE_STUDENT) {
            queryWrapper.eq("sid", user.getId());
        } else if (user.getType() == Constast.USER_TYPE_TEACHER) {
            queryWrapper.eq("mgr", user.getId());
        }
        this.page(leaveIPage, queryWrapper);
        return new DataGridView(leaveIPage.getTotal(), leaveIPage.getRecords());
    }

    /**
     * 根据假单状态查询
     */
    @Override
    public DataGridView loadAllLeaveByStatus(LeaveVo leaveVo, Integer status) {
        IPage<Leave> leaveIPage = new Page<>(leaveVo.getPage(), leaveVo.getLimit());
        QueryWrapper<Leave> queryWrapper = new QueryWrapper<>();
        User user = (User) WebUtils.getSession().getAttribute("user");
        queryWrapper.eq("status", status);
        if (user.getType() == Constast.USER_TYPE_STUDENT) {
            queryWrapper.eq("sid", user.getId());
        } else if (user.getType() == Constast.USER_TYPE_TEACHER) {
            queryWrapper.eq("mgr", user.getId());
        } else if (user.getType() == Constast.USER_TYPE_TUTOR||user.getType()==Constast.USER_TYPE_DEPTLEADER) {
            queryWrapper.eq("deptid", user.getDeptid());
        }
        this.page(leaveIPage, queryWrapper);
        return new DataGridView(leaveIPage.getTotal(), leaveIPage.getRecords());
    }

    /**
     * 根据假单状态查询已审批的假单
     */
    @Override
    public DataGridView loadAllLeaveApprovalCheck(LeaveVo leaveVo) {
        IPage<Leave> leaveIPage = new Page<>(leaveVo.getPage(), leaveVo.getLimit());
        QueryWrapper<Leave> queryWrapper = new QueryWrapper<>();
        User user = (User) WebUtils.getSession().getAttribute("user");
        if (user.getType() == Constast.USER_TYPE_STUDENT) {
            queryWrapper.eq("sid", user.getId());
        } else if (user.getType() == Constast.USER_TYPE_TEACHER) {
            queryWrapper.eq("mgr", user.getId());
            queryWrapper.ne("status", 0);
        }else if(user.getType() == Constast.USER_TYPE_TUTOR||user.getType()==Constast.USER_TYPE_DEPTLEADER){
            queryWrapper.eq("deptid", user.getDeptid());
            queryWrapper.ne("status", 0);
        }
        this.page(leaveIPage, queryWrapper);
        return new DataGridView(leaveIPage.getTotal(), leaveIPage.getRecords());
    }


    /**
     * 添加
     */
    @Override
    public ResultObj addLeave(LeaveVo leaveVo) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date releaseDate = null;
        try {
            releaseDate = format.parse(leaveVo.getRawstartdate());
            leaveVo.setStartdate(releaseDate);
            releaseDate = format.parse(leaveVo.getRawenddate());
            leaveVo.setEnddate(releaseDate);
            leaveVo.setCreatetime(new Date());
            leaveVo.setStatus(0);
            this.save(leaveVo);
            return ResultObj.APPLY_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.APPLY__ERROR;
        }
    }

    /**
     * 修改
     */
    @Override
    public ResultObj updateLeave(LeaveVo leaveVo) {
        try {
            leaveVo.setStatus(1);
            this.updateById(leaveVo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除
     */
    @Override
    public ResultObj deleteLeave(Integer id) {
        try {
            this.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 批量删除
     */
    @Override
    public ResultObj batchDeleteLeave(LeaveVo leaveVo) {
        try {
            Collection<Serializable> idList = new ArrayList<Serializable>();
            for (Integer id : leaveVo.getIds()) {
                idList.add(id);
            }
            this.removeByIds(idList);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 同意
     */
    @Override
    public ResultObj agreeLeave(Integer id) {
        try {
            LeaveVo leaveVo = leaveMapper.getLeaveById(id);
            leaveVo.setStatus(2);
            this.updateById(leaveVo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 销假
     */
    @Override
    public ResultObj backoutLeave(Integer id) {
        try {
            LeaveVo leaveVo = leaveMapper.getLeaveById(id);
            leaveVo.setStatus(3);
            this.updateById(leaveVo);
            return ResultObj.AGREE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.AGREE_ERROR;
        }
    }

    @Override
    public boolean save(Leave entity) {
        // TODO Auto-generated method stub
        return super.save(entity);
    }

    @Override
    public boolean updateById(Leave entity) {
        // TODO Auto-generated method stub
        return super.updateById(entity);
    }

    @Override
    public boolean removeById(Serializable id) {
        // TODO Auto-generated method stub
        return super.removeById(id);
    }

    @Override
    public Leave getById(Serializable id) {
        // TODO Auto-generated method stub
        return super.getById(id);
    }

}
